package com.bionic.banking.dao.classes;

import com.bionic.banking.core.entity.BankTransactionType;
import com.bionic.banking.dao.IBankTransactionTypeDao;
import com.bionic.banking.dao.connection.DBQueryManager;
import java.sql.*;
import java.util.List;

public class BankTransactionTypeDaoImpl implements IBankTransactionTypeDao {
    
    protected final String SQL_SELECT = "SELECT * FROM " + getTableName();
    protected final String SQL_INSERT = "INSERT INTO " + getTableName() + "(NAME, DESCRIPTION) VALUES(?,?)";
    protected final String SQL_UPDATE = "UPDATE " + getTableName() + " SET NAME=?, DESCRIPTION=? ";
    protected final String SQL_DELETE = "DELETE FROM " + getTableName();
    protected final String SQL_WHERE = " WHERE " + getKeyColumn() + "=?";
    protected static final int COLUMN_ID = 1;
    protected static final int COLUMN_NAME = 2;
    protected static final int COLUMN_DESCRIPTION = 3;
    protected int maxRows;

    @Override
    public String getTableName() {
        return "BANK_TRANSACTIONTYPE";
    }

    @Override
    public String getKeyColumn() {
        return "TX_TYPE_ID";
    }

    @Override
    public DBQueryManager<BankTransactionType> getDBManager() {
        return new DBQueryManager<>(this);
    }

    @Override
    public BankTransactionType populateDto(ResultSet rs) throws SQLException {
        BankTransactionType dto = new BankTransactionType();
        dto.setTxTypeId(rs.getInt(COLUMN_ID));
        dto.setName(rs.getString(COLUMN_NAME));
        dto.setDescription(rs.getString(COLUMN_DESCRIPTION));
        return dto;
    }

    @Override
    public Integer create(BankTransactionType type) throws SQLException {
        Object[] params = new Object[]{
            type.getName(),
            type.getDescription()
        };
        Integer newId = getDBManager().executeCreateQuery(SQL_INSERT, params);
        type.setTxTypeId(newId);//update with new generated PK in DB 
        return newId;
    }

    @Override
    public boolean update(BankTransactionType type) throws SQLException {
        Object[] params = new Object[]{
            type.getName(),
            type.getDescription(),
            type.getTxTypeId()
        };
        final String query = SQL_UPDATE + SQL_WHERE;
        return getDBManager().executeSaveQuery(query, params);
    }

    @Override
    public void remove(int id) throws SQLException {
        final String query = SQL_DELETE + SQL_WHERE;
        getDBManager().executeRemoveQuery(query, id);
    }

    @Override
    public BankTransactionType findById(int id) throws SQLException {
        String query = SQL_SELECT + SQL_WHERE;
        Object[] params = new Object[]{new Integer(id)};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public BankTransactionType findByName(String name) throws SQLException {
        String query = SQL_SELECT + " WHERE NAME = ?";
        Object[] params = new Object[]{name};
        return getDBManager().executeSelectSingleQuery(query, params);
    }

    @Override
    public List<BankTransactionType> findAll() throws SQLException {
        String query = SQL_SELECT;
        return getDBManager().executeSelectQuery(query, null);
    }

    @Override
    public void setMaxRows(int maxRows) {
        this.maxRows = maxRows;
    }

    @Override
    public int getMaxRows() {
        return maxRows;
    }
}